$(function () {
    var linkman_datagrid = $("#linkman_datagrid");
    var linkman_dialog = $("#linkman_dialog");
    var linkman_form = $("#linkman_form");
    linkman_datagrid.datagrid({
        url: '/linkman/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'id', title: '编号', width: 100,},
            {field: 'name', title: '姓名', width: 100,},
            {
                field: 'sex', title: '性别', width: 100, formatter: function (value) {
                return value ? "男" : "女"
            }
            },
            {field: 'birthday', title: '生日', width: 100},
            {
                field: 'school', title: '学校', width: 100, formatter: function (value) {
                return value ? value.school : "";
            }
            },
            {field: 'department', title: '部门', width: 100},
            {field: 'duty', title: '职务', width: 100},
            {field: 'tel', title: '电话', width: 100},
            {field: 'qq', title: 'QQ', width: 100},
            {field: 'email', title: '邮箱', width: 100},
            {
                field: 'main', title: '是否主联系人', width: 100, formatter: function (value) {
                return value ? "是" : "否"
            }
            },
            {field: 'remark', title: '备注', width: 100}
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#linkman_btns"
    });
    linkman_dialog.dialog({
        width: 300,
        height: 550,
        buttons: "#save_btns",
        closed: true,
        onClose: function () {
            linkman_form.form('clear');
        }
    });
    $("#import_dialog").dialog({
        title: '文件上传',
        width: 200,
        height: 200,
        closed: true,
        buttons: '#import_btns',
        onClose: function () {
            $("#import_form").form('clear');
        }
    })
    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObject[methodName]();
    })
    var methodObject = {
        add: function () {
            linkman_dialog.dialog('setTitle', '新增学校联系人');
            linkman_dialog.dialog('open');
        },
        edit: function () {
            var row = linkman_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            row['school.id'] = row.school.id;
            linkman_form.form('load', row
            );
            linkman_dialog.dialog('setTitle', '编辑学校联系人');
            linkman_dialog.dialog('open');
        },
        reload: function () {
            linkman_datagrid.datagrid('reload');
        },
        cancel: function () {
            linkman_dialog.dialog('close');
        },
        save: function () {
            linkman_form.form('submit', {
                url: '/linkman/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info')
                        methodObject.cancel();
                        linkman_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },

        query: function () {
            var keyword = $("[name=keyword]").val();
            linkman_datagrid.datagrid('load', {
                keyword: keyword

            })
        },
        del:function () {

            var row = linkman_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框','您确认要删除吗',function (r) {
                if (r){
                    $.get('/linkman/delete.do',{id:row.id},function (data) {
                        if (data.success){
                            $.messager.alert('温馨提示','删除成功','info',function () {
                                methodObject.cancel();
                                linkman_datagrid.datagrid('reload');
                            })
                        }else {
                            $.messager.alert('温馨提示',data.msg,'error');
                        }
                    })
                }
            });
        },
        exportXls: function () {
            var keyword = $("[name=keyword]").val();
            window.location.href = '/linkman/exportXls.do?keyword=' + keyword;
        },
        import: function () {
            $("#import_dialog").dialog('open');
        },
        import_save: function () {
            $("#import_form").form('submit', {
                url: '/linkman/importXls.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '上传成功', 'info');
                        methodObject.import_cancel();
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },
        import_cancel: function () {
            $("#import_dialog").dialog('close');
        }
    }
})


